/**
 * 
 */
package com.net.web.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.net.domain.MarkDetails;
import com.net.domain.Staff;
import com.net.domain.StudentAttendanceDetail;
import com.net.service.MarkService;
import com.net.service.UtilService;
import com.net.web.form.MarksForm;

/**
 * @author Dinesh
 *
 */
public class MarkListController extends MultiActionController {

	final static Logger log = Logger.getLogger(MarkListController.class);
	
	private static int sequenceCounter = 0;
	final static SimpleDateFormat formatter = new SimpleDateFormat("MM/d/yyyy");
	
	private static HashMap<Integer, List<MarkDetails>> cacheForStudentList = new HashMap<Integer, List<MarkDetails>>();
	
	private MarkService markService;
	
	private UtilService utilService;
	
	public ModelAndView enterMarksForm(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView("enterMarks");
						
		Staff staff = (Staff)req.getSession().getAttribute("user");
			
		MarksForm searchQuery=new MarksForm();
		//mav.addObject("studentList",markService.getAllEligibleStudentsByGroup(searchQuery));
		mav.addObject("classMaps", utilService.getClassByStaff(staff.getReferenceId()));
		mav.addObject("subjects",utilService.getAllSubject());
		mav.addObject("tests",utilService.getAllCycleTest());
		
		return mav;		
	}
	public ModelAndView writeMarksForm(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView("writeMarks");
		
		log.info("got into write marks form");
		int cycleTestId=Integer.parseInt(req.getParameter("cycleTestId"));
		int subjectId=Integer.parseInt(req.getParameter("subjects"));

		MarksForm searchQuery = new MarksForm();
		
/*		if(req.getParameter("type").equalsIgnoreCase("any")) {
			String combinedClass = req.getParameter("class");
			String [] classSem = combinedClass.split("-");
			
			searchQuery.setSemester(classSem[0]);
			searchQuery.setSectionName(classSem[1]);
			searchQuery.setCouseShortCode(req.getParameter("course"));
			searchQuery.setSubjectCode(req.getParameter("subject"));
		}
		else {
			searchQuery.setSubjectClassGroupId(Integer.parseInt(req.getParameter("class_map")));
		}*/
		
	
		
		searchQuery.setSubjectClassGroupId(Integer.parseInt(req.getParameter("class_map")));
		
		searchQuery.setTestId(Integer.parseInt(req.getParameter("cycleTestId")));
				
		List<MarkDetails> markList =markService.getEligibleStudentsByGroupTest(searchQuery) ; 
		
		int key = getSequence();
		
		if(markList.size() > 0) {
			//StudentAttendanceDetail studentanceDetail = attendanceList.get(0);
			log.info("mark");
			cacheForStudentList.put(key, markList);
		}
		mav.addObject("randomkey", key);
		mav.addObject("cycleTestId",cycleTestId);
		mav.addObject("subjectId",subjectId);
		mav.addObject("students",markList);
		mav.addObject("totalMarks", req.getParameter("total_marks"));

		
		return mav;

}
	public ModelAndView writeMarksForm1(HttpServletRequest req, HttpServletResponse res) throws Exception {

	    int sequence = Integer.parseInt(req.getParameter("randomkey"));

	    List<MarkDetails> markList = cacheForStudentList.get(sequence);
	    int cycleTestId=Integer.parseInt(req.getParameter("cycleTestId"));
	    int subjectId=Integer.parseInt(req.getParameter("subjectId"));
	    int totalMarks=Integer.parseInt(req.getParameter("totalMarks").trim());
	    
		if(markList != null)
			cacheForStudentList.remove(sequence);
for(MarkDetails studentDetail: markList) {
			
			//TODO: CHeck whether class_hour_mapping is required
	studentDetail.setCycleTestId(cycleTestId);
	studentDetail.setTotalMarks(totalMarks);
	studentDetail.setSubjectId(subjectId);
	double scoredMarks=Double.parseDouble(req.getParameter("scored_marks_"+studentDetail.getStudentId()).trim());
    studentDetail.setScoredMarks(scoredMarks);
			
			
}
markService.insertMarkDetails(markList);
		return enterMarksForm(req,res);
		
	
	}

	private int getSequence() {
		// TODO Auto-generated method stub
		return sequenceCounter++;
	}
	public MarkService getMarkService() {
		return markService;
	}
	@Autowired
	public void setMarkService(MarkService markService) {
		this.markService = markService;
	}
	public UtilService getUtilService() {
		return utilService;
	}
	@Autowired
	public void setUtilService(UtilService utilService) {
		this.utilService = utilService;
	}
}